<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 积分管理
 * @author viviqin
 *
 */
class Credit extends CI_Controller {
	
	private $tab = 'user';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	private function get_credit_data($limit=false){
		$username 	= $this->input->get('username');
		$this->load->model('User_model');
		
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;
		$is_self 	= isset($_GET['is_self']) ? $this->input->get('is_self') : -1;
		
		$this->load->model('credit_model');
		
		$user_id = 0;
		if($username != ''){
			$user_id = $this->User_model->get_userid_by_username($username);
		}
		
		$per_page  = intval($this->input->get('per_page'));
		$page_size = YTZ_LIST_PAGE_SIZE;
		$parms     = array(
			'user_id'   => intval($user_id),
		);
		
		if($is_loan != '' && $is_loan != -1){
			$parms['is_loan'] = $is_loan;
		}
		if($is_self != '' && $is_self != -1){
			$parms['is_self'] = $is_self;
		}
		
		$vdata = $this->credit_model->get_credit_list_all($parms,$per_page,$page_size,$limit);
		$vdata['username'] = $username;
		$vdata['is_loan'] 	= $is_loan;
		$vdata['is_self'] 	= $is_self;
		
		$this->load->model('Account_model');
		$vdata['user_loan'] = $this->Account_model->arrays("user_loan");
		$vdata['user_self'] = $this->Account_model->arrays("user_self");
		
		return $vdata;
	}
	
	private function get_credit_log_data($limit=false){
		$this->load->model('credit_model');
		$type_id	= $this->input->get('type_id');
		$op			= $this->input->get('op');
		$username 	= $this->input->get('username');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$user_id = 0;
		if($username != ''){
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
		}
		
		$per_page  = intval($this->input->get('per_page'));
		$page_size = YTZ_LIST_PAGE_SIZE;
		$parms     = array(
			'user_id'   => intval($user_id),
			'op'      => intval($this->input->get('op')),
			'type_id'	 => intval($this->input->get('type_id')),
			'strtime'	 => $this->input->get('strtime'),
			'endtime'	 => $this->input->get('endtime')
		);
		
		$vdata = $this->credit_model->get_credit_log_list($parms,$per_page,$page_size,$limit);
		
		
		$vdata['creditTypeName'] = $this->credit_model->get_credit_rule_list_name();
		
		//$vdata['creditTypeName'] = $this->credit_model->arrays("creditTypeName");
		$vdata['creditOpName'] = $this->credit_model->arrays("creditOpName");
		$vdata['type_id'] = $type_id;
		$vdata['op'] = $op;
		$vdata['username'] = $username;
		$vdata['strtime'] = $strtime;
		$vdata['endtime'] = $endtime;
		
		return $vdata;
	}
	
	function clist(){
		$vdata = $this->get_credit_data();
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/credit/clist/?username='.$vdata['username'];
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'clist';
		
		$this->load->view('/ychadmin/credit_list',$vdata);
	}
	
	function clist_excle(){
		$vdata = $this->get_credit_data('all');
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("积分列表")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("积分编号", "用户名称", "总积分", "可用积分", "不可用积分");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		
		$_list = $vdata['list'];
		
		for($i = 0; $i < count($_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, number_format($_list[$i]['credit_value'],2));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, number_format($_list[$i]['available'],2));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, number_format($_list[$i]['no_available'],2));
		}
		
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="用户积分.xls"');
		header('Cache-Control: max-age=0');
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		$objWriter->save('php://output');
		exit;
	}
	
	function creditlog(){
		
		$vdata = $this->get_credit_log_data();
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/credit/creditlog/?username='.$vdata['username'].'&type_id='.$vdata['type_id'].'&op='.$vdata['op'].'&strtime='.$vdata['strtime'].'&endtime='.$vdata['endtime'];
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'clog';
		
		$this->load->view('/ychadmin/credit_list_log',$vdata);
	}
	
	function creditlog_excle(){
		
		$vdata = $this->get_credit_log_data('all');
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("积分日志列表")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("日志编号", "用户名称", "积分类型", "变动类型", "变动积分数值", "备注","时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		
		$_list = $vdata['list'];
		
		for($i = 0; $i < count($_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $vdata['creditTypeName'][$_list[$i]['type_id']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $vdata['creditOpName'][$_list[$i]['op']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, number_format($_list[$i]['credit_value'],2));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $_list[$i]['remark']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, date('Y-m-d H:i:s', $_list[$i]['add_time']));
		}
		
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="积分日志.xls"');
		header('Cache-Control: max-age=0');
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		$objWriter->save('php://output');
		exit;
	}
	
	
	function credit_view(){
		$this->load->model('credit_model');
		#$vdata['creditTypeName'] = $this->credit_model->arrays("creditTypeName");
                $vdata['creditTypeName'] = $this->credit_model->get_credit_rule_list_name();
		$vdata['creditOpName'] = $this->credit_model->arrays("creditOpName");
		
		$vdata['tab'] = 'user';
		$vdata['sub'] = 'credit';
		
		$this->load->view('/ychadmin/credit_view',$vdata);
	}
	
	
	function deduct(){
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		$username 		= $this->input->post('username');
		$available 			= $this->input->post('available');
		$type_id 	= $this->input->post('type_id');
		$op 	= $this->input->post('op');
		$remark 		= $this->input->post('remark');
		
		$user_id = 0; 
		if(!$username){
			$ret['msg'] = '请填写用户名';
		}elseif(!$this->myformcheck->IsDouble($available)){
			$ret['msg'] = '请填写有效的积分';
		}elseif(!$type_id){
			$ret['msg'] = '请选择积分类型';
		}elseif(!$op){
			$ret['msg'] = '请选择变动类型';
		}elseif(!$remark){
			$ret['msg'] = '请填写备注';
		}else{
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			if($user_id < 1){
				$ret['msg'] = '用户名不存在';
			}
		}
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
		
		$this->load->model('Credit_model', 'credit');
		
		$log = array(
			'user_id' => $user_id,
			'type_id' => $type_id,
			'op' => $op,
			'credit_value' => $available,
			'remark' => $remark,
			'op_user' => 1,
			'add_time' => time(),
			'addip' => $this->js->get_ip(),
		);
					
			
		if($op == 1){
			$res = $this->credit->incr_credit($log);
		}elseif($op == 2){
			$res = $this->credit->decr_credit($log);
		}					
	
		if($res > 0){
			$ret['msg'] = 'success';
			$this->load->model('log_model');$this->log_model->add_log('[添加][增加/扣除积分--成功]'.'[用户名:'.$username.',积分:'.$available.']');
		}else{
			$ret['msg'] = 'fail';
			$this->load->model('log_model');$this->log_model->add_log('[添加][增加/扣除积分--失败]'.'[用户名:'.$username.',积分:'.$available.']');
		}
   		echo json_encode($ret);
	}
	
	function crule(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		
		$this->load->model('Credit_model');
		$vdata = $this->Credit_model->credit_type_list($per_page,$page_size);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/credit/crule?';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'crule';		
		$this->load->view('/ychadmin/credit_rule', $vdata);
	}
	
	function credit_rule_save(){
		$this->load->model('Credit_model');
		
		$admin = $this->session->userdata('admin_user');
		if(isset($admin['type_id']) && $admin['type_id'] ==1){
			$act = $this->input->post("act");
			
			$parms = array(	'name'=>$this->input->post("name"),
							'credit_value'=>$this->input->post("credit_value"),
						);
								
			if($act == 'add'){
				$parms['nid'] = $this->input->post("nid");
				$res = $this->Credit_model->add_credit_rule($parms);
			}else if($act == 'delete'){
				$id = $this->input->post("id");
				$res = $this->Credit_model->del_credit_rule($id);
			}else{
				$id = $this->input->post("id");
				$res = $this->Credit_model->update_credit_rule($id,$parms);
			}
			
			if($res>0){
				$ret = array('msg'=>'操作成功');
			}else{
				$ret = array('msg'=>'操作失败');
			}
		}else{
			$ret = array('msg'=>'对不起，权限不足！');
		}
		
		echo json_encode($ret);
		exit; 
	}
}